package com.bckj.fastboot.iot.biz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bckj.fastboot.core.lang.model.PageResult;
import com.bckj.fastboot.core.page.PageSupport;
import com.bckj.fastboot.iot.biz.convert.IotDeviceConvert;
import com.bckj.fastboot.iot.biz.repository.IotDeviceRepository;
import com.bckj.fastboot.iot.biz.service.IotDeviceService;
import com.bckj.fastboot.iot.common.dto.query.IotDeviceQuery;
import com.bckj.fastboot.iot.common.dto.request.IotDeviceRequest;
import com.bckj.fastboot.iot.common.dto.response.IotDeviceResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * IotDeviceServiceImpl
 *
 * @author wfc
 **/
@Service
@RequiredArgsConstructor
public class IotDeviceServiceImpl implements IotDeviceService {

    private final IotDeviceRepository iotDeviceRepository;
    private final IotDeviceConvert iotDeviceConvert;

    @Override
    public boolean save(IotDeviceRequest request) {
        var entity = iotDeviceConvert.toEntity(request);
        entity.setCreateId(1L);
        return iotDeviceRepository.save(entity);
    }

    @Override
    public boolean update(IotDeviceRequest request) {
        var entity = iotDeviceConvert.toEntity(request);
        return iotDeviceRepository.updateById(entity);
    }

    @Override
    public IotDeviceResponse details(Long id) {
        return iotDeviceRepository.getBaseMapper().findById(id);
    }

    @Override
    public List<IotDeviceResponse> findList(IotDeviceQuery query) {
        return iotDeviceRepository.getBaseMapper().findList(query);
    }

    @Override
    public PageResult<IotDeviceResponse> page(IPage<IotDeviceResponse> page, IotDeviceQuery query) {
        var result = PageSupport.page(page, query, this::findList);
        return PageResult.of(result);
    }
}
